var web3 = new Web3(Web3.givenProvider ||"ws://localhost:8546");//如果没有连接到metamask生成web3实例就会连接ws网路端口号为8566的节点生成web3实例
console.log("Web3", web3);
//得到节点的所有账户并输出
let accounts=[];
 web3.eth.getAccounts()
    .then(function (metamaskAccounts){
        accounts=metamaskAccounts;
        console.log(accounts[0]);
    });
    
//得到某个账户的余额并输出
web3.eth.getBalance("0xbD7eFb6A883186bf6Bd53Ad1DcA58465276cfCac").then(console.log);

//放入合约的abi
const abi = [
	{
		"inputs": [],
		"stateMutability": "nonpayable",
		"type": "constructor"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			},
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"name": "_addrToIds",
		"outputs": [
			{
				"internalType": "bytes32",
				"name": "",
				"type": "bytes32"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			}
		],
		"name": "_enterpriseToName",
		"outputs": [
			{
				"internalType": "string",
				"name": "",
				"type": "string"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "bytes32",
				"name": "",
				"type": "bytes32"
			}
		],
		"name": "_idToInfo",
		"outputs": [
			{
				"internalType": "string",
				"name": "name",
				"type": "string"
			},
			{
				"internalType": "uint256",
				"name": "state",
				"type": "uint256"
			},
			{
				"internalType": "uint256",
				"name": "batchId",
				"type": "uint256"
			},
			{
				"internalType": "string",
				"name": "intro",
				"type": "string"
			},
			{
				"internalType": "string",
				"name": "traceInfo",
				"type": "string"
			},
			{
				"internalType": "uint256",
				"name": "periodOfValidity",
				"type": "uint256"
			},
			{
				"internalType": "bool",
				"name": "isBuy",
				"type": "bool"
			},
			{
				"internalType": "bool",
				"name": "isUse",
				"type": "bool"
			},
			{
				"internalType": "bool",
				"name": "isArriveStorageEnterprise",
				"type": "bool"
			},
			{
				"internalType": "bool",
				"name": "isArriveDeliveryEnterprise",
				"type": "bool"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			}
		],
		"name": "_isAuthDeliveryEnterprise",
		"outputs": [
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			}
		],
		"name": "_isAuthHospital",
		"outputs": [
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			}
		],
		"name": "_isAuthStorageEnterprise",
		"outputs": [
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "_produceEnterprise",
		"outputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "bytes32",
				"name": "id",
				"type": "bytes32"
			},
			{
				"internalType": "string",
				"name": "time",
				"type": "string"
			},
			{
				"internalType": "address",
				"name": "addr",
				"type": "address"
			}
		],
		"name": "arriveDeliveryEnterprise",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "bytes32",
				"name": "id",
				"type": "bytes32"
			},
			{
				"internalType": "string",
				"name": "time",
				"type": "string"
			},
			{
				"internalType": "address",
				"name": "addr",
				"type": "address"
			}
		],
		"name": "arriveHospital",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "bytes32",
				"name": "id",
				"type": "bytes32"
			},
			{
				"internalType": "string",
				"name": "time",
				"type": "string"
			},
			{
				"internalType": "address",
				"name": "addr",
				"type": "address"
			}
		],
		"name": "arriveStorageEnterprise",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "addr",
				"type": "address"
			},
			{
				"internalType": "uint256",
				"name": "temp",
				"type": "uint256"
			},
			{
				"internalType": "string",
				"name": "name",
				"type": "string"
			}
		],
		"name": "choiceEnterprise",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "addr",
				"type": "address"
			}
		],
		"name": "getLengthOfVaccine",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "addr",
				"type": "address"
			}
		],
		"name": "getLengthOfVactoryVaccine",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "getaa",
		"outputs": [
			{
				"internalType": "bytes32[]",
				"name": "",
				"type": "bytes32[]"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "string",
				"name": "name",
				"type": "string"
			},
			{
				"internalType": "uint256",
				"name": "state",
				"type": "uint256"
			},
			{
				"internalType": "uint256",
				"name": "batchId",
				"type": "uint256"
			},
			{
				"internalType": "uint256",
				"name": "periodOfValidity",
				"type": "uint256"
			},
			{
				"internalType": "string",
				"name": "intro",
				"type": "string"
			},
			{
				"internalType": "uint256",
				"name": "num",
				"type": "uint256"
			},
			{
				"internalType": "string",
				"name": "time",
				"type": "string"
			}
		],
		"name": "produce",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "bytes32",
				"name": "id",
				"type": "bytes32"
			}
		],
		"name": "useVaccine",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
]
//合约的地址
const contractAddress = "0x78f0eCc3fbf6430A90976895e6e7BbCCF3f66b21";
//生成自己合约的实例对象
export default new web3.eth.Contract(abi,contractAddress);
//疫苗生产函数

$('#produce').click(function(){
	var name = $('#name').val();
	var state = $('#state').val();
    var batchId = $('#batchId').val();
    var periodOfValidity = $('#periodOfValidity').val();
    var intro = $('#intro').val();
    var num = $('#num').val();
    var time = $('#time').val();
    myContract.methods.produce(name,state,batchId,periodOfValidity,intro,num,time)
    .send({
    from: accounts[0],
     gas: 150000,
     gasPrice: 3000000000,
     value: 3000000
    })
})
// 选定企业函数，第一个参数为选定企业的地址，第二个参数为0为选定仓储企业、第二个参数为1为选定配送企业、第二个参数为2为选定医院，第三个参数为选定企业的名称。
$('#choiceEnterprise').click(function(){
	var addr = $('#addr').val();
	var temp = $('#temp').val();
    var name1 = $('#name1').val();
myContract.methods.choiceEnterprise(addr,temp,name1).send({
    form:accounts[0],
    gas: 150000,
    gasPrice: 3000000000,
    value: 3000000
    })
})
// 到达仓储企业函数
$('#arriveStorageEnterprise').click(function(){
	var id = $('#id').val();
	var time1 = $('#time1').val();
    var addr1 = $('#addr1').val();
myContract.methods.arriveStorageEnterprise(id,time1,addr1).send({
    form:accounts[0],
    gas: 150000,
    gasPrice: 3000000000,
    value: 3000000
    })
})
//到达运输企业函数
$('#arriveDeliveryEnterprise').click(function(){
    var id1 = $('#id1').val();
	var time2 = $('#time2').val();
    var addr2 = $('#addr2').val();
myContract.methods.arriveDeliveryEnterprise(id1,time2,addr2).send({
    form:accounts[0],
    gas: 150000,
    gasPrice: 3000000000,
    value: 3000000
    })
})
// 到达医院函数
$('#choiceEnterprise').click(function(){
	var id2 = $('#id2').val();
	var time3 = $('#time3').val();
    var addr3 = $('#addr3').val();
myContract.methods.arriveHospital(id2,time3,addr3).send({
    form:accounts[0],
    gas: 150000,
    gasPrice: 3000000000,
    value: 3000000
    })
})
//查询溯源哈希
$('#choiceEnterprise').click(function(){
    var adress = $('#address').val();
    var arrays = $('#arrays').val();
myContract.methods._addrToIds(adress,arrays).call().then(function (result) {
    console.log("result =", result);
    //document.getElementById("result").value = res.result
    })
})
// // 地址对应的疫苗Id数组长度(为展示而设计)
// myContract.methods.getLengthOfVaccine(addr).call().then(function (result) {
//     console.log("result =", result);
// })
// // 查看厂家/医院的总疫苗库存数量
// myContract.methods.getLengthOfVactoryVaccine(addr).call().then(function (result) {
//     console.log("result =", result);
// })
// //工厂
// myContract.methods._produceEnterprise.call().then(function (result) {
//     console.log("result =", result);
// })
// 使用疫苗函数: 疫苗标记为已使用
// myContract.methods.useVaccine(id) .call().then(function (result) {
//     console.log("result =", result);
// })
$('#choiceEnterprise').click(function(){
    var bytes32 = $('#address').val();
myContract.methods._idToInfo(bytes32).call().then(function (result) {
    console.log("result =", result);
    //document.getElementById("result").value = res.result
    })
})